package com.passer.toolbox.common.file.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * <p>文件复制工具类</p>
 * <p>创建时间：2022/8/11</p>
 *
 * @author hanjie
 */
public class CopyFileUtils {

    /**
     * 复制文件
     *
     * @param filePath     原文件路径
     * @param targetFolder 目标文件夹路径
     */
    public static void copyFile(String filePath, String targetFolder) {
        File file = new File(filePath);
        String fileName = file.getName();
        File targetFile = new File(targetFolder, fileName);
        try (
                FileChannel in = new FileInputStream(file).getChannel();
                FileChannel out = new FileOutputStream(targetFile, true).getChannel()
        ) {
            out.transferFrom(in, 0, in.size());
        } catch (IOException e) {
            throw new RuntimeException("文件拷贝失败：" + e.getMessage());
        }
    }

}
